package com.CampusLife.controller;

import com.CampusLife.bean.Login;
import com.CampusLife.bean.LostComment;
import  com.CampusLife.bean.LostProduct;
import com.CampusLife.bean.LostProductType;
import com.CampusLife.service.ILostCommentService;
import com.CampusLife.service.ILostProductTypeService;
import com.CampusLife.until.DeleteImages;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import  com.CampusLife.service.ILostProductService;
import com.CampusLife. until.DataTime;
import  com.CampusLife.until.ImagesUpload;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;

/**
 * @author Administrator
 */
@Controller
@RequestMapping("/lostProduct")
public class LostProductController {
    @Resource
    private ILostProductService iLostProductService;
    @Resource
    private ILostProductTypeService iLostProductTypeService;
    @Resource
    private ILostCommentService iLostCommentService;

    @RequestMapping("/queryAll")
    public  String  findall(Model model){
        List<LostProduct> lostProducts=iLostProductService.getProductAll();
        model.addAttribute("lostProducts",lostProducts);
        List<LostProductType> list=iLostProductTypeService.queryAll();
        model.addAttribute("list",list);
        return  "admin/lostbasic";
    }
    @RequestMapping("/query")
    public  String  query(Model model, @RequestParam(defaultValue="1",required=true,value="pageNo") Integer pageNo){
        Integer pageSize=15;//每页显示记录数
        PageHelper.startPage(pageNo, pageSize);
        List<LostProduct> LostProducts=iLostProductService.queryAll();
        PageInfo<LostProduct> pageInfo=new PageInfo<LostProduct>(LostProducts);
        model.addAttribute("pageInfo",pageInfo);
        List<LostProductType> lostProductTypes=iLostProductTypeService.queryAll();
        model.addAttribute("lostProductTypes",lostProductTypes);
        int sort=1;
        model.addAttribute("sort",sort);
        return  "home/shiwulist";
    }
    @RequestMapping("/queryByName")
    public  String  queryByName(Model model,String lost_name,@RequestParam(defaultValue="1",required=true,value="pageNo") Integer pageNo){
        Integer pageSize=15;//每页显示记录数
        PageHelper.startPage(pageNo, pageSize);
        List<LostProduct> LostProducts= iLostProductService.queryByName(lost_name);
        PageInfo<LostProduct> pageInfo=new PageInfo<LostProduct>(LostProducts);
        model.addAttribute("pageInfo",pageInfo);
        List<LostProductType> lostProductTypes=iLostProductTypeService.queryAll();
        model.addAttribute("lostProductTypes",lostProductTypes);
        int sort=2;
        model.addAttribute("sort",sort);
        model.addAttribute("lost_name",lost_name);
        return  "home/shiwulist";
    }
    @RequestMapping("inAddJsp")
    public String inAddJsping(Model model){
        List<LostProductType> lostProductTypes=iLostProductTypeService.queryAll();
        model.addAttribute("lostProductTypes",lostProductTypes);
        return  "home/addlost";
    }
    @RequestMapping(path = "/addLostProduct", method = RequestMethod.POST)
    @ResponseBody
    public int addUser(@RequestParam("file") MultipartFile file, HttpServletRequest request,
                          HttpServletResponse response, HttpSession httpSession,ModelMap model, Integer l_tid, String lost_name, String describe_txt, String phone) throws Exception {
        if (file.isEmpty()) {
            return -500;
        }
        else if (!file.isEmpty()){
        ImagesUpload imagesUpload =new ImagesUpload();
        String photo=  imagesUpload.Upload(file);
            DataTime dataTime=new DataTime();
            LostProduct lostProduct=new LostProduct();
            lostProduct.setLost_name(lost_name);
            lostProduct.setL_tid(l_tid);
            lostProduct.setDescribe_txt(describe_txt);
            lostProduct.setPhone(phone);
            lostProduct.setTime(dataTime.gettime());
            int i=(int)httpSession.getAttribute("id");
            lostProduct.setId(i);
            lostProduct.setPhoto(photo);
          iLostProductService.add(lostProduct);
        return 0;}
        else {
            return 1;
        }
    }
    //跳转到修改页面
    @RequestMapping("/jumpUpdateLostProductEdit")
    public String jumpUpdateLostProductEdit(Integer l_id,Model model){
        List<LostProductType> list=iLostProductTypeService.queryAll();
        model.addAttribute("lostProductTypes",list);
        LostProduct lostProduct=iLostProductService.queryById(l_id);
        model.addAttribute("lostProduct",lostProduct);
        return "home/updatelost";
    }
    //修改·    /图片上传
    @RequestMapping(path = "/updateLostProduct", method = RequestMethod.POST)
    @ResponseBody
    public int updataProduct(@RequestParam("file") MultipartFile file, HttpServletRequest request,
                          HttpServletResponse response, HttpSession httpSession,ModelMap model,Integer l_id ,Integer l_tid, String lost_name, String describe_txt, String phone) throws Exception {

       if (!file.isEmpty()){
        ImagesUpload imagesUpload =new ImagesUpload();
        String photo=  imagesUpload.Upload(file);
            DataTime dataTime=new DataTime();
            LostProduct lostProduct=new LostProduct();
            lostProduct.setL_id(l_id);
            lostProduct.setLost_name(lost_name);

            lostProduct.setDescribe_txt(describe_txt);
            lostProduct.setPhone(phone);
            lostProduct.setTime(dataTime.gettime());
            int i=(int)httpSession.getAttribute("id");
            lostProduct.setId(i);
            lostProduct.setPhoto(photo);


            iLostProductService.update(lostProduct);

        return 0;
        }else if (file.isEmpty()) {
            return -500;
        }
        else {
            return 1;
        }
    }
    @RequestMapping("/delete")
    public String delete(Integer l_id,Model model)throws Exception{
        LostProduct lostProduct=iLostProductService.getLostProductById(l_id);
        DeleteImages deleteImages=new DeleteImages();
        File url= new File(lostProduct.getPhoto());
        deleteImages.deleteDirectory(url);
        iLostProductService.delete(l_id);
        return "redirect:/lostProduct/queryAll";
    }
    @RequestMapping("/userBydelete")
    public String userBydelete(Integer l_id,Model model){
        iLostProductService.delete(l_id);
        return "redirect:/lostProduct/getLostProductByUserId";
    }

    @RequestMapping("/getProductAllByLostName")
    public String getProductAllByLostName(String lost_name,Model model) {
        LostProduct lostProduct = new LostProduct();
        lostProduct.setLost_name(lost_name);
        List<LostProduct> lostProducts = iLostProductService.getProductAllByLostName(lostProduct);
        model.addAttribute("lostProducts", lostProducts);
        List<LostProductType> list = iLostProductTypeService.queryAll();
        model.addAttribute("list", list);
        return "admin/lostbasic";
    }
    @RequestMapping("/getLostProductByType")
    public String getLostProductByType(Integer l_tid,Model model){
        List<LostProduct> lostProducts = iLostProductService.getLostProductByType(l_tid);
        model.addAttribute("lostProducts", lostProducts);
        List<LostProductType> list = iLostProductTypeService.queryAll();
        model.addAttribute("list", list);
        return "admin/lostbasic";
    }
    @RequestMapping("/getLostProductByType0")
    public String getLostProductByType0(Integer l_tid,Model model,@RequestParam(defaultValue="1",required=true,value="pageNo") Integer pageNo){
        Integer pageSize=15;//每页显示记录数
        PageHelper.startPage(pageNo, pageSize);
        List<LostProduct> lostProducts = iLostProductService.getLostProductByType(l_tid);
        PageInfo<LostProduct> pageInfo=new PageInfo<LostProduct>(lostProducts);
        model.addAttribute("pageInfo",pageInfo);
        List<LostProductType> lostProductTypes=iLostProductTypeService.queryAll();
        model.addAttribute("lostProductTypes",lostProductTypes);;
        int sort=3;
        model.addAttribute("sort",sort);
        model.addAttribute("l_tid",l_tid);
        return "home/shiwulist";
    }
    @RequestMapping("/getLostProductById")
    public  String  getLostProductById(Model model,int l_id,HttpSession httpSession){
        LostProduct  lostProducts = iLostProductService.getLostProductById(l_id);
        model.addAttribute("lostProducts",lostProducts);
        List<LostComment> lostComments= iLostCommentService.queryById(l_id);
        model.addAttribute("lostComments",lostComments);
        int userId=(int)httpSession.getAttribute("id");
        model.addAttribute("userId",userId);
        return  "home/lostproduct";
    }
    @RequestMapping("/getLostProductByUserId")
    public String getLostProductByUserId(Model model,HttpSession httpSession){
        int userId=(Integer) httpSession.getAttribute("id");
        List<LostProduct> lostProducts=iLostProductService.getLostProductByUserId(userId);
        model.addAttribute("lostProducts",lostProducts);
        return "home/user-lost";

    }
}
